//
//  SampuxBubbleView.m
//  Sampux
//
//  Created by rzm on 16/8/24.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxBubbleView.h"

@interface SampuxBubbleView ()

@property (nonatomic, strong) UIImageView *bubbleView;

@end

@implementation SampuxBubbleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 16.;
    }
    _bubbleView = [[UIImageView alloc]initWithFrame:CGRectMake(self.width - Sampux6WScale(100) , 64, Sampux6WScale(100), Sampux6WScale(107))];
    _bubbleView.image = imageNamed(@"bubble");
    _bubbleView.userInteractionEnabled = YES;
    [self addSubview:_bubbleView];
    NSArray *imgArr = @[@"line_house",@"transfer"];
    NSArray *titArr = @[@"返回首页",@"权限转移"];
    
    for (int i = 0; i < 2; i ++) {
        UIButton *btn = [UIButton createButtonWithImage:imageNamed(imgArr[i]) frame:CGRectMake(3,(_bubbleView.height*0.35 +Sampux6WScale(10)) *i + Sampux6WScale(10)+ Sampux6WScale(10), _bubbleView.width, _bubbleView.height* 0.35 )];
        [_bubbleView addSubview:btn];
        btn.tag = i;
        btn.titleLabel.font = THFont(font - 2);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, - 5, 0, 5);
        [btn setTitle:titArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:THBlackAlplaColor(0.8) forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(bubbleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)bubbleBtnClick:(UIButton *)bubbleBtn
{
    [self removeBubbleView];

    if (bubbleBtn.tag == 0) {
        if (self.backBlock) {
            self.backBlock();
        }
//        [self.navigationController popToRootViewControllerAnimated:YES];
    }else
    {
        if (self.addBlock) {
            self.addBlock();
        }
//        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
//        delegate.window.backgroundColor = THSystemBlackColor;
//        [delegate.window addSubview:self.transferView];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGFloat ly = [touch locationInView:  self.bubbleView].y;
    if ( ly < 0 || ly > self.bubbleView.height ) { //点标题栏也会消失，点其他地方捕捉不到了
        [self removeBubbleView];
    }
    [super touchesBegan:touches withEvent:event];  
}

- (void)removeBubbleView
{
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    window.backgroundColor = [UIColor clearColor];
    window.alpha = 1;
    // 2.添加自己到窗口上
    UIWindow *window1 = [[UIApplication sharedApplication].windows lastObject];
    window1.backgroundColor = [UIColor clearColor];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}

@end
